package com.utooo.service.quality;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.utooo.dao.purchase.OrderReceiptDetailMapper;
import com.utooo.dao.purchase.OrderReceiptMapper;
import com.utooo.dao.quality.InspectionTaskMapper;
import com.utooo.pojo.domain.purchase.order.OrderReceiptDO;
import com.utooo.pojo.domain.purchase.order.OrderReceiptDetailDO;
import com.utooo.pojo.domain.quality.InspectionTaskDO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static com.utooo.constant.Constans.IGNORED_PROPERTIES;

@Slf4j
@Service
public class InspectionTaskServiceImpl extends ServiceImpl<InspectionTaskMapper, InspectionTaskDO>
        implements InspectionTaskService {

    @Resource
    private OrderReceiptMapper orderReceiptMapper;

    @Resource
    private OrderReceiptDetailMapper orderReceiptDetailMapper;


    @Override
    public void addInspectionTask(String orderReceiptId) {
        OrderReceiptDO orderReceipt =
                orderReceiptMapper.selectById(orderReceiptId);
        if (orderReceipt == null) {
            log.error("订单收货为空:{}", orderReceiptId);
            return;
        }
        List<OrderReceiptDetailDO> orderReceiptDetails = orderReceiptDetailMapper
                .selectList(new LambdaQueryWrapper<OrderReceiptDetailDO>()
                .eq(OrderReceiptDetailDO::getForeignId, orderReceiptId));
        if (CollUtil.isEmpty(orderReceiptDetails)) {
            log.error("订单收货明细为空:{}", orderReceiptId);
            return;
        }
        orderReceiptDetails.forEach(i -> {
            InspectionTaskDO inspectionTaskDO = new InspectionTaskDO();
            BeanUtil.copyProperties(orderReceipt, inspectionTaskDO, IGNORED_PROPERTIES);
            inspectionTaskDO.setInspectionNo(generateInspectionNo());
            inspectionTaskDO.setReceiptNo(orderReceiptId);
            // 质检类型
//            inspectionTaskDO.setInspectionType("1");
            inspectionTaskDO.setSupplierName(orderReceipt.getSupplierName());
            inspectionTaskDO.setArrivalDate(LocalDateTime.now());
            inspectionTaskDO.setMaterialCode(i.getMaterialCode());
            inspectionTaskDO.setMaterialName(i.getMaterialName());
            inspectionTaskDO.setCategoryCode(i.getCategoryCode());
            inspectionTaskDO.setCategoryName(i.getCategoryName());
            inspectionTaskDO.setReceiptDetailId(i.getId());
            this.save(inspectionTaskDO);
        });
    }


    private String generateInspectionNo() {
        String prefix = "GR";
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        String datePart = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));

        // 查询当天已有的最大订单号
        LambdaQueryWrapper<InspectionTaskDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.likeRight(InspectionTaskDO::getInspectionNo, prefix + datePart);
        queryWrapper.orderByDesc(InspectionTaskDO::getInspectionNo);
        queryWrapper.last("LIMIT 1");
        InspectionTaskDO inspectionTaskDO = this.getOne(queryWrapper);

        // 生成序列号
        int maxSequence = 0;
        if (inspectionTaskDO != null) {
            String inspectionNo = inspectionTaskDO.getInspectionNo();
            maxSequence = Integer.parseInt(inspectionNo.substring((prefix + datePart).length()));
        }
        int nextSequence = maxSequence + 1;
        String sequencePart = String.format("%03d", nextSequence);
        return prefix + datePart + sequencePart;
    }
}